package com.example.consumptionrecords.controller;

import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisListResult;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.example.consumptionrecords.entity.dto.TextToImgDTO;
import com.example.consumptionrecords.entity.enums.GlobalConstants;
import com.example.consumptionrecords.entity.http.Response;
import com.example.consumptionrecords.entity.pojo.User;
import com.example.consumptionrecords.exception.GlobalException;
import com.example.consumptionrecords.util.dashscope.DashScopeUtil;
import com.example.consumptionrecords.util.qianfan.QianFanUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * hhzr9
 * 2024 \ 03 \ 28
 */

@Slf4j
@RestController
@RequestMapping(value = "/aimode")
public class AiModeController {

	@Resource
	private QianFanUtil qianFanUtil;

	@Resource
	private DashScopeUtil dashScopeUtil;

	@PostMapping("/qianfan/chat")
	public ResponseEntity<Response<String>> chat(@RequestParam(value = "message")String message) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		try {
			log.info("用户：{} 使用千帆对话", user.getUserPhone());
			String qianfanChat = qianFanUtil.qianfanChatStream(message);
			log.info("用户：{} 使用千帆对话成功", user.getUserPhone());
			return ResponseEntity.ok(Response.success(qianfanChat));
		} catch (GlobalException e) {
			log.error("用户：{} 使用千帆对话失败: {}", user.getUserPhone(), e.getMessage());
			return ResponseEntity.ok(Response.error(e.getCode(), e.getMessage()));
		} catch (Exception e) {
			log.error("用户：{} 使用千帆对话接口出错: ", user.getUserPhone(), e);
			return ResponseEntity.ok(Response.error());
		}
	}

	@PostMapping("/dashscope/textToImg")
	public ResponseEntity<Response<ImageSynthesisResult>> textToImg(@RequestBody TextToImgDTO textToImgDTO) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		try {
			log.info("用户：{} 使用阿里文字转图片", user.getUserPhone());
			ImageSynthesisResult imageSynthesisResult = dashScopeUtil.textToImg(textToImgDTO);
			log.info("用户：{} 使用阿里文字转图片成功", user.getUserPhone());
			return ResponseEntity.ok(Response.success(imageSynthesisResult));
		} catch (ApiException e) {
			log.info("用户：{} 使用阿里文字转图片失败: {}, {}", user.getUserPhone(), e.getMessage(), e.getStatus());
			return ResponseEntity.ok(Response.error(GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_API_ERR.getCode(), GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_API_ERR.getMessage()));
		} catch (NoApiKeyException e) {
			log.info("用户：{} 使用阿里文字转图片apikey出错: {}", user.getUserPhone(), e.getMessage());
			return ResponseEntity.ok(Response.error(GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_NO_API_KEY_ERR.getCode(), GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_NO_API_KEY_ERR.getMessage()));
		} catch (Exception e) {
			log.info("用户：{} 使用阿里文字转图片接口出错: ", user.getUserPhone(), e);
			return ResponseEntity.ok(Response.error());
		}
	}

	@PostMapping("/dashscope/listTask")
	public ResponseEntity<Response<ImageSynthesisListResult>> listTask() {
		try {
			log.info("获取所有文生图任务");
			ImageSynthesisListResult imageSynthesisListResult = dashScopeUtil.listTask();
			log.info("获取所有文生图任务成功");
			return ResponseEntity.ok(Response.success(imageSynthesisListResult));
		} catch (ApiException e) {
			log.error("获取所有文生图任务失败: 状态{}", e.getStatus());
			return ResponseEntity.ok(Response.error(GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_ALL_TASK_API_ERR.getCode(), GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_ALL_TASK_API_ERR.getMessage()));
		} catch (NoApiKeyException e) {
			log.error("获取所有文生图任务失败: {}", e.getMessage());
			return ResponseEntity.ok(Response.error(GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_NO_API_KEY_ERR.getCode(), GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_NO_API_KEY_ERR.getMessage()));
		} catch (Exception e) {
			log.error("获取所有文生图任务失败: ", e);
			return ResponseEntity.ok(Response.error());
		}
	}

	@PostMapping("/dashscope/fetchTask/{taskId}")
	public ResponseEntity<Response<ImageSynthesisResult>> fetchTask(@PathVariable String taskId) {
		try {
			log.info("获取文生图任务: {}", taskId);
			ImageSynthesisResult imageSynthesisResult = dashScopeUtil.fetchTask(taskId);
			log.info("获取文生图任务成功");
			return ResponseEntity.ok(Response.success(imageSynthesisResult));
		} catch (ApiException e) {
			log.error("获取文生图任务失败: 状态{}", e.getStatus());
			return ResponseEntity.ok(Response.error(GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_TASK_API_ERR.getCode(), GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_TASK_API_ERR.getMessage()));
		} catch (NoApiKeyException e) {
			log.error("获取文生图任务失败: {}", e.getMessage());
			return ResponseEntity.ok(Response.error(GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_NO_API_KEY_ERR.getCode(), GlobalConstants.DASH_SCOPE_TEXT_TO_IMG_NO_API_KEY_ERR.getMessage()));
		} catch (Exception e) {
			log.error("获取文生图任务失败: ", e);
			return ResponseEntity.ok(Response.error());
		}
	}
}
